package br.edu.ufcg.msnlab2.grupo08.euler;

import java.util.ArrayList;
import java.util.List;

import br.edu.ufcg.msnlab2.misc.Function;
import br.edu.ufcg.msnlab2.misc.Point;
import br.edu.ufcg.msnlab2.misc.PointImpl;

public class EulerModified {

	private final Function equation;

	public EulerModified(Function equation) {
		this.equation = equation;
		
	}
	
	/**
	 * @param a - Initial value of X
	 * @param b - Last value of X
	 * @param y0 - Initial value of Y
	 * @param steps - number of steps
	 * 
	 * @return results for each (x,y)
	 */
	public List<Point> eulerModified( double a, double b, double y0, int steps ) {
		double h = (b-a)/steps;
		List<Point> results = new ArrayList<Point>();
		double x = a;
		double y = equation.evaluate(x);
		results.add(new PointImpl(x, y));
		for(int i = 1; i <= steps; i++) {
			y = y + (h/2)*(deriv(x,y) + deriv(x+h, y+h*deriv(x,y))); 
			x += h;
			results.add(new PointImpl(x, y));
		}
		return results;
		
	}
	
	// The derivative dy/dx at a given value of x and y.
	protected double deriv(double x, double y){
	    return equation.evaluate(x, y);
	 }

}
